package fr.zenexity.transportInParis;

import java.io.IOException;
import java.util.Hashtable;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class MetroInParis extends MIDlet implements CommandListener {

    private Display display;
    private BrowsingCanvas canvas;
    private Command metroCommand = new Command("Metro", Command.SCREEN, 0);
    private Command busCommand = new Command("Bus", Command.SCREEN, 1);
    private Hashtable imageMap = new Hashtable();

    public MetroInParis() {
        this.display = Display.getDisplay(this);
    }

    private void init() {
        this.canvas = new BrowsingCanvas(this.createImage("/metro.png"));
        this.canvas.setCommandListener(this);
        this.canvas.addCommand(metroCommand);
        this.canvas.addCommand(busCommand);
    }

    private Image createImage(String imagePath) {
        Image image = (Image) this.imageMap.get(imagePath);
        if (image == null) {
            try {
                image = Image.createImage(imagePath);
                this.imageMap.put(imagePath, image);
            } catch (IOException e) {
                // it never happens..
            }
        }
        return image;
    }

    public void commandAction(Command c, Displayable d) {
        if (c == metroCommand) {
            this.canvas.changeImage(this.createImage("/metro.png"));
        }
        if (c == busCommand) {
            this.canvas.changeImage(this.createImage("/bus_paris.png"));
        }
    }

    // �v�nement ex�cut� au d�marrage de l'application
    public void startApp() {

        //affichage de l'écran de load
        Alert alert = new Alert(null, null, this.createImage("/toto.png"), AlertType.INFO);
        alert.setTimeout(2000);
        this.init();
        this.display.setCurrent(alert, this.canvas);
    }

    public void alertDebug(Throwable e) {
        Alert alert = new Alert("debug", e.getMessage(), null, null);
        this.display.setCurrent(alert);
    }

    // �v�nement ex�cut� lors de la mise en pause de l'application
    public void pauseApp() {
    }

    // �v�nement ex�cut� lorsque l'application se termine
    public void destroyApp(boolean unconditional) {
    }
}